package com.jwork.progress.service;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ProducerService<T> {
    public void send() {
        KafkaProducer<String, String> kafkaProducer = null;
        try {
            Map<String, Object> configMap = new HashMap<>();
            configMap.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "10.1.0.253:9092,10.1.0.253:9093,10.1.0.253:9094");
            configMap.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
            configMap.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

            kafkaProducer = new KafkaProducer<String, String>(configMap);
            ProducerRecord<String, String> sendRecord = new ProducerRecord<String, String>("First", "first send key",
                    "first send value");
            kafkaProducer.send(sendRecord);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            kafkaProducer.close();
        }
    }
}
